﻿/************************************************************************
 * Chương trình Demo bài toán Mã Đi Tuần                                
 * Form chính của chương trình, vẽ và hiển thị từng bước đi của quân Mã 
 * lên bàn cờ vua
 ************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Ma_Di_Tuan.Properties;
using System.Media;
using System.Diagnostics;

namespace Ma_Di_Tuan
{
    //Thời gian
    struct TimeStruct
    {
        public int second;
        public int minute;
        public int hour;
    };

    public partial class Main : Form
    {
        private Label[,]        lNumber;                    //mảng các label hiển thị toạn độ của bàn cờ
        private PictureBox[,]   pbaBoard;                      //Mảng Bàn Cờ
        private Board     controlBoard;               //Bảng ddieuf khiển
        private int             size;                 //Kích thước bàn cờ
        private int             initX;
        private int             initY;
        private int             widthSize = 50;

        public Main()
        {
            InitializeComponent();

            this.BackgroundImage        = global::Ma_Di_Tuan.Properties.Resources.bamboo_back;
            this.BackgroundImageLayout  = ImageLayout.Stretch;
            this.Icon                   = global::Ma_Di_Tuan.Properties.Resources.horse;
        }

        public void SetInitValue(int _X, int _Y, int pSize)
        {
            initX = _X;
            initY = _Y;
            size = pSize;
        }

        public void Init()
        {
            DrawBoard();

            controlBoard = new Board();
            controlBoard.SetValue(initX, initY, size, pbaBoard);
            controlBoard.InitControl();
            controlBoard.MouseWheel += new MouseEventHandler(groupHandle_MouseWheel);
            this.Controls.Add(controlBoard);
            
            this.Width              = 50 + widthSize * size + controlBoard.Width;
            this.WindowState        = FormWindowState.Normal;
            if (size <= 10)
            {
                AutoScroll          = false;
                FormBorderStyle     = FormBorderStyle.FixedSingle;
                this.WindowState    = FormWindowState.Normal;
                MaximizeBox         = false;
                controlBoard.Height  = 521;
                this.Height         =  controlBoard.Height;
            }
            else
            {
                AutoScroll          = true;
                controlBoard.Height  = 555;
                this.Width          += 20; //width of scroll

                if (size == 12)
                {
                    //34 height of title, 20 width of scroll
                    this.Height         = 2 * lNumber[1, 1].Height + size * widthSize + 34 + 20;
                    this.WindowState    = FormWindowState.Normal;
                }
                else
                {
                    this.Height             = Screen.PrimaryScreen.WorkingArea.Height - 5;
                    this.WindowState        = FormWindowState.Maximized;
                    this.FormBorderStyle    = FormBorderStyle.Sizable;
                    MaximizeBox             = true;
                }
            }
        }

        private void DrawBoard()
        {
            lNumber = new Label[2, size];
            for (int i = 0; i < size; i++)
            {
                //horizontal
                lNumber[1, i] = new Label();
                lNumber[1, i].Font      = new Font("Microsoft Sans Serif", 12F, FontStyle.Bold);
                lNumber[1, i].BackColor = Color.Transparent;
                lNumber[1, i].ForeColor = Color.White;
                lNumber[1, i].AutoSize  = true;
                lNumber[1, i].Location  = new System.Drawing.Point(widthSize * (i + 1) - 5, 9);
                lNumber[1, i].Name      = i.ToString();
                lNumber[1, i].Size      = new System.Drawing.Size(19, 13);
                lNumber[1, i].TabIndex  = 1;
                lNumber[1, i].Text      = (i + 1).ToString();
                this.Controls.Add(lNumber[1, i]);

                //verticle
                lNumber[0, i]           = new Label();
                lNumber[0, i].Font      = new Font("Microsoft Sans Serif", 12F, FontStyle.Bold);
                lNumber[0, i].BackColor = Color.Transparent;
                lNumber[0, i].ForeColor = Color.White;
                lNumber[0, i].AutoSize  = true;
                lNumber[0, i].Location  = new System.Drawing.Point(1, widthSize * (i + 1) - 5);
                lNumber[0, i].Name      = i.ToString();
                lNumber[0, i].Size      = new System.Drawing.Size(19, 13);
                lNumber[0, i].TabIndex  = 1;
                lNumber[0, i].Text      = (i + 1).ToString();
                this.Controls.Add(lNumber[0, i]);
            }

            PictureBox picKhungVien              = new PictureBox();
            picKhungVien.Location                = new System.Drawing.Point(30, 30);
            picKhungVien.BackgroundImage         = global::Ma_Di_Tuan.Properties.Resources.chessboard;
            picKhungVien.BackgroundImageLayout   = ImageLayout.Stretch;
            picKhungVien.BackColor               = Color.White;
            picKhungVien.Width                   = size * widthSize + 12;
            picKhungVien.Height                  = picKhungVien.Width;
            this.Controls.Add(picKhungVien);

            //draw
            pbaBoard = new PictureBox[size + 1, size + 1];
            for (int i = 1; i <= size; i++)
            {
                for (int j = 1; j <= size; j++)
                {
                    pbaBoard[i, j] = new PictureBox();

                    //white or black
                    if (i % 2 != 0)
                    {
                        if (j % 2 != 0)
                        {
                            pbaBoard[i, j].BackgroundImage = global::Ma_Di_Tuan.Properties.Resources.white_back;
                        }
                        else
                        {
                            pbaBoard[i, j].BackgroundImage = global::Ma_Di_Tuan.Properties.Resources.black_back;
                        }
                    }
                    else
                    {
                        if (j % 2 != 0)
                        {
                            pbaBoard[i, j].BackgroundImage = global::Ma_Di_Tuan.Properties.Resources.black_back;
                        }
                        else
                        {
                            pbaBoard[i, j].BackgroundImage = global::Ma_Di_Tuan.Properties.Resources.white_back;
                        }
                    }

                    //set properties for picture box array.
                    pbaBoard[i, j].Location = new System.Drawing.Point(6 + (j - 1) * widthSize, 6 + (i - 1) * widthSize);
                    pbaBoard[i, j].Name = "pictureBox1";
                    pbaBoard[i, j].Size = new System.Drawing.Size(widthSize, widthSize);
                    pbaBoard[i, j].SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                    pbaBoard[i, j].TabIndex = 0;
                    pbaBoard[i, j].TabStop = false;
                    pbaBoard[i, j].MouseHover += new EventHandler(Main_MouseHover);
                    picKhungVien.Controls.Add(pbaBoard[i, j]);
                }
            }
        }

        private void Main_FormClosing(object sender, FormClosingEventArgs e)
        {
            Settings.Default.strTime    = controlBoard.GetTimeCombobox();
            Settings.Default.Save();

            Application.Exit();
        }

        //Khi Scroll Form thì di chuyển bảng Điều khiển chạy theo
        private void Main_Scroll(object sender, ScrollEventArgs e)
        {
            controlBoard.Top = 0;
        }

        private void Main_MouseHover(object sender, EventArgs e)
        {
            controlBoard.Top = 0;
        }

        void groupHandle_MouseWheel(object sender, MouseEventArgs e)
        {
            controlBoard.Top = 0;
        }
    }
}
